package org.example.util;

import java.io.*;
import java.net.URL;
import java.nio.file.Files;

public class FileUtil {
    public static String getFilePath(String relativePath){
        URL resource = FileUtil.class.getResource("/");
        String dir = resource == null? "" : resource.getPath();
        return dir + relativePath;
    }


    public static byte[] readBytes(String filePath){
        File file = new File(filePath);
        if(!file.exists()){
            throw new IllegalArgumentException(filePath + "not exist");
        }

        InputStream in = null;

        try {
            in = Files.newInputStream(file.toPath());
            in = new BufferedInputStream(in);
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            IOUtil.copy(in,bao);
            return bao.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            IOUtil.closeIO(in);
        }

        return null;
    }
}
